package youleche.com.interfaces;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import youleche.com.common.ParamConfigParameters;
import youleche.com.sql.servlet.common.SuperHttpServlet;
import youleche.com.util.AESEncryptAndDecrypt;

/**
 * 用户登陆接口(用于用户登陆状态)
 * @author Administrator
 *
 */
public class LoginAction extends SuperHttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginAction() {
        super();
    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int action = Integer.parseInt(request.getParameter("action") == null ? "0" : request.getParameter("action"));
		switch(action){
		case 0: 
			///判断用户是否登录
			this.isLogin(request, response);
			break;
		}
	}
	
	/**
	 * 判断用户是否登录
	 * @param request
	 * @param response
	 */
	private void isLogin(HttpServletRequest request, HttpServletResponse response){
		super.response(response, isLoginToInner(request, response));
	}
	
	/**
	 * 获取用户登录名
	 * @param request
	 * @param response
	 * @return
	 */
	public String getLoginName(HttpServletRequest request, HttpServletResponse response){
		Cookie[] cookies = request.getCookies();
		if(cookies == null || cookies.length == 0 ||cookies.length == 1){
			return "";
		}
		
		String userName = "";
		for(int i=0 ;i<cookies.length;i++){
			if(!cookies[i].getName().equals("JSESSIONID") && "youleche.u".equals(cookies[i].getName())){
				userName = cookies[i].getValue();
			}
		}
		
		return userName;
	}
	
	/**
	 * 判断用户是否登录
	 * @param request
	 * @param response
	 */
	public String isLoginToInner(HttpServletRequest request, HttpServletResponse response){
		Cookie[] cookies = request.getCookies();
		if(cookies == null || cookies.length == 0 ||cookies.length == 1){
			return "false";
		}
		
		String result = "false";
		for(int i=0 ;i<cookies.length;i++){
			if(!cookies[i].getName().equals("JSESSIONID") && "youleche.s".equals(cookies[i].getName())){
				String cookieVlaue = cookies[i].getValue();
				ParamConfigParameters paramConfig = new ParamConfigParameters();
				String loginCode_KEY = paramConfig.getParameter("loginCode_KEY");
				try {
					String nameAndPass = AESEncryptAndDecrypt.decrypt(cookieVlaue, loginCode_KEY);
					if(!nameAndPass.contains("^")){
						result = "false";
						break;
					}

					String sessionKey = (String) request.getSession().getAttribute(""+nameAndPass.split("\\^")[0]+"");
					if("".equals(sessionKey)){
						break;
					}
					
					if(cookieVlaue.equals(sessionKey)){
						result = "true";
						break;
					}else{
						result = "false";
						break;
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				break;
			}
		}
		
		return result;
	}
	
	/**
	 * 判断用户是否登录
	 * @param request request 对象
	 * @param response response对象
	 * @return 返回boolean值，true登录 FALSE未登录
	 */
	public boolean is_Login(HttpServletRequest request, HttpServletResponse response){
		return "true".equals(this.isLoginToInner(request, response));
	}

}
